//
//  HMTabBar.swift
//  Weibo
//
//  Created by ZhuDong on 16/5/19.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class HMTabBar: UITabBar {

    var btnClickClosure: (()->())?
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.addSubview(self.btn)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        let w = self.bounds.size.width / 5
        let h = self.bounds.size.height
        let rect = CGRect(x: 0, y: 0, width: w, height: h)
        var index: CGFloat = 0
        for subview in self.subviews {
    if  (subview.isKindOfClass(NSClassFromString("UITabBarButton")!)) {
                if index == 2 {
                    index += 1
                }
                subview.frame = rect.offsetBy(dx: w * index, dy: 0)
                index += 1
            }

        }
        self.btn.frame = rect.offsetBy(dx: w * 2, dy: 0)
        
    }
    //控件用闭包实现懒加载
    private lazy var btn: UIButton = {()->(UIButton) in
       
        let btn = UIButton()
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add" + "_highlighted"), forState: .Highlighted)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button" + "_highlighted"), forState: .Highlighted)
        btn.addTarget(self, action: #selector(btnClick), forControlEvents: .TouchUpInside)
        return btn
    }()
    
   @objc private func btnClick() -> () {
        //
        btnClickClosure?()
    }
}

